/**
 * SEVAS - Swiss Electronic Vaccination Advisor Service / Agile Development
 *
 * $Id: Patient.java 55 2010-09-01 18:12:20Z zimmr7@bfh.ch $
 */
package ch.bfh.sevas;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;

@Entity
public class Patient implements Serializable {

    public final static int MALE = 1;
    public final static int FEMALE = 0;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id = 0L;

    private String surname;
    private String givenName;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateOfBirth;
    private int sex;            // Sex of the client (0 - female, 1 - male)
    private String ssn;         // social security number

    @OneToMany(cascade=CascadeType.ALL)
    private List<Vaccination> vaccinations;

    public Patient() {
        vaccinations = new ArrayList();
    }

    public Patient(String surname, String givenName, Date dateOfBirth, int sex, String ssn) {
        this();

        this.surname = surname;
        this.givenName = givenName;
        this.dateOfBirth = dateOfBirth;
        this.sex = sex;
        this.ssn = ssn;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    public int getSex() {
        return sex;
    }

    public String getSexAsString() {
        return (isFemale() ? "female" : "male");
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public boolean isFemale() {
        return (sex == Patient.FEMALE);
    }

    public void setFemale(boolean female) {
        this.sex = female ? Patient.FEMALE : Patient.MALE;
    }

    public boolean isMale() {
        return (sex == Patient.MALE);
    }

    public void setMale(boolean male) {
        this.sex = male ? Patient.MALE : Patient.FEMALE;
    }


    public String getSsn() {
        return ssn;
    }

    public void setSsn(String ssn) {
        this.ssn = ssn;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Long getId() {
        return id;
    }

    public void addVaccination(Vaccination vaccination) {
        vaccinations.add(vaccination);
    }

    public void removeVaccination(Vaccination vaccination) {
        vaccinations.remove(vaccination);
    }

    public List<Vaccination> getVaccinations() {
        return vaccinations;
    }

    @Override
    public String toString() {
        return "Patient(" + id + ", " + surname + ", " + givenName + ", " + new DateConverter().convertForward(dateOfBirth) + ", " + sex + ", " + ssn + ")";
    }

}